# koroFileHeader at Home PC
# Create: 2021-04-10 17:33:09
# LastEdit: 2021-04-11 09:56:51
"""获取所有的订阅"""
__author__ = '749B'

import os, sys
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 
sys.path.append(BASE_DIR)

from msrestazure.azure_cloud import AZURE_CHINA_CLOUD as CLOUD
from azure.mgmt.resource import SubscriptionClient
from azure.identity import DefaultAzureCredential

# 加载环境变量
from environs import Env
env = Env()
env.read_env()

credential = DefaultAzureCredential(authority=CLOUD.endpoints.active_directory)
subscription_client = SubscriptionClient(
    credential,
    base_url=CLOUD.endpoints.resource_manager,
    credential_scopes=[CLOUD.endpoints.resource_manager + "/.default"])

subscriptions = [sub.as_dict() for sub in subscription_client.subscriptions.list()]

for sub in subscriptions:
    print('    {name} = {id},'.format(
        name=sub.get("display_name").ljust(20), 
        id=sub.get("subscription_id")))


